package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.SubjectTwoResult;
import com.gljx.web.Entity.SubjectTwoResultentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.dao.SubjectTwoResultMapper;
import com.gljx.web.dao.SubjectTwoResultentryMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.SubjectTwoResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SubjectTwoResultServiceImpl extends CommonDao implements SubjectTwoResultService {
	@Autowired
	private SubjectTwoResultMapper  subjectTwoResultMapper;
	
	@Autowired
	private SubjectTwoResultentryMapper subjectTwoResultentryMapper;
	
	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private CustomLogService customLogService;
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectTwoResultMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectTwoResultMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectTwoResultMapper.selectFnumber(companyId);
	}

	@Override
	public int deleteSubjectTwoResult(String fid) {
		// TODO Auto-generated method stub
		return subjectTwoResultMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public int submitSubjectTwoResult(SubjectTwoResult subjectTwoResult) {
		return subjectTwoResultMapper.submitSubjectTwoResult(subjectTwoResult);
	}

	@Override
	public int addSubjectTwoResult(User user,SubjectTwoResult subjectTwoResult, String[] stuid,String [] stufseq ,String[] stucfzkzmbh,
			String[] stufnumber, String[] stuname, String[] stucfidcardno, String[] stucfsex, String[] stucfsjhm,
			String[] stucflxdh, String[] stucfslzjcxdh, String[] stucfclazzName, String[] stucfamount,
			String[] stucfreceived, String[] stucfarrearage, String[] stucffavorable, String[] stucfvip,
			String[] stucflocal, String[] stucfexamresult, String[] stucfremark) {
		// TODO Auto-generated method stub
		int count=0;
		//首先先添加父类
		count+=subjectTwoResultMapper.insertSelective(subjectTwoResult);
		//添加子类
		for (int i = 0; i < stuid.length; i++) {
			SubjectTwoResultentry subjectTwoResultentry=new SubjectTwoResultentry();
			subjectTwoResultentry.setFseq(Long.valueOf(stufseq[i]));
			subjectTwoResultentry.setCfzkzmbh(stucfzkzmbh[i]);
			subjectTwoResultentry.setFid(GetFid.UUIDIntoFid());
			subjectTwoResultentry.setFparentid(subjectTwoResult.getFid());
			subjectTwoResultentry.setCfstudentid(stuid[i]);
			subjectTwoResultentry.setStudentName(stuname[i]);
			subjectTwoResultentry.setCfidcardno(stucfidcardno[i]);
			subjectTwoResultentry.setCfsex(Long.valueOf(stucfsex[i]));
			subjectTwoResultentry.setCfmobile(stucfsjhm[i]);
			subjectTwoResultentry.setCfphone(stucflxdh[i]);			
			subjectTwoResultentry.setCfsqzjcxdh(stucfslzjcxdh[i]);
			subjectTwoResultentry.setCfstufnumber(stufnumber[i]);
			subjectTwoResultentry.setCfclazz(stucfclazzName[i]);
			subjectTwoResultentry.setCfreceivable(new BigDecimal(stucfamount[i]));
			subjectTwoResultentry.setCfreceived(new BigDecimal(stucfreceived[i]));
			subjectTwoResultentry.setCfarrearage(new BigDecimal(stucfarrearage[i]));
			subjectTwoResultentry.setCffavorable(new BigDecimal(stucffavorable[i]));
			subjectTwoResultentry.setCfvip(Long.valueOf(stucfvip[i]));
			subjectTwoResultentry.setCfbdyd(Long.valueOf(stucflocal[i]));
			subjectTwoResultentry.setCfexamresult(Long.valueOf(stucfexamresult[i]));
			subjectTwoResultentry.setCfremark(stucfremark[i]);
			count+=subjectTwoResultentryMapper.insertSelective(subjectTwoResultentry);
			// 添加学员服务日志
			customLogService.addLog("考试管理", "新增了学员:"+subjectTwoResultentry.getCfstufnumber()+subjectTwoResultentry.getStudentName()
					+",科目二考试结果登记!申请车型:"+subjectTwoResultentry.getCfsqzjcxdh()+",考试日期:"+subjectTwoResult.getFbizdate()+",单据编号:"+subjectTwoResult.getFnumber(), subjectTwoResultentry.getCfstudentid(), user.getFid());
		}
		return count;
	}

	@Override
	public SubjectTwoResult selSubjectTwoResultByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectTwoResultMapper.selectSubjectTwoResultByFid(fid);
	}

	@Override
	public int updateSubjectTwoResult(SubjectTwoResult subjectTwoResult) {
		// TODO Auto-generated method stub
		int row1=0;
		int row2=0;
		//审核单据
		row1=subjectTwoResultMapper.updateByPrimaryKeySelective(subjectTwoResult);
		subjectTwoResult.setSubjectTwoResultentryList(subjectTwoResultentryMapper.selSubjectTwoResultentryByParentId(subjectTwoResult.getFid(), null,null));
		List<SubjectTwoResultentry> subjectTwoResultentryList=subjectTwoResult.getSubjectTwoResultentryList();
		//修改学员状态 科目二考试时间 和考试次数
        //获取该学员的科目二考试次数
		//获取当前修改时间
        String updateTime = TimeFormat.timeFormat(new Date());           
        for (int i = 0; i < subjectTwoResultentryList.size(); i++) {
			Student _student=studentMapper.selectStudentById(subjectTwoResultentryList.get(i).getCfstudentid());
			Student student=new Student();
			student.setCfkmekss(_student.getCfkmekss());
			student.setFid(subjectTwoResultentryList.get(i).getCfstudentid());
			if(student.getCfkmekss() == null){//如果等于null设置值为0
				student.setCfkmekss(0L);
			}
			if(student.getCfkmekss() < 5){
				student.setCfkmekss(student.getCfkmekss()+1L);//次数加一
				if(subjectTwoResultentryList.get(i).getCfexamresult() == 1){//合格						
					//student.setCfkmehgrq(updateTime);//科目二合格日期
					//student.setCfstudentstate(4L);//状态为科目二通过
					//student.setCfkmekss(_student.getCfkmekss()+1L);//科目二考试次数加一
					student.setCfkmehgrq(TimeFormat.timeFormat(new Date()));//科目二通过时间
					if(_student.getCfkmshgrq() !=null){//先判断科目三通过时间是否存在 
						student.setCfstudentstate(7L);//科目二通过 科目三通过									
					}else if(_student.getCfkmsbjgsj() !=null || _student.getCfkmsksqksj()!=null){//判断该学员的未通过科目三时间 和缺考时间									
						student.setCfstudentstate(8L);//科目二通过 科目三未通过									
					}else{//如果全部都为空则说明该学员未开始过科目三
						student.setCfstudentstate(4L);//科目二通过 科目三未开始									
					}
				}else if(subjectTwoResultentryList.get(i).getCfexamresult() == 2){//不合格
					//student.setCfkmebjgsj(updateTime);//科目二不及格时间
					//student.setCfstudentstate(5L);//状态为科目二未通过
					//student.setCfkmekss(_student.getCfkmekss()+1L);//科目二考试次数加一
					student.setCfkmebjgsj(TimeFormat.timeFormat(new Date()));//科目二未通过时间
					if(_student.getCfkmshgrq() !=null){//先判断科目三通过时间是否存在 
						student.setCfstudentstate(17L);//科目二未通过 科目三通过									
					}else if(_student.getCfkmsbjgsj() !=null || _student.getCfkmsksqksj()!=null){//判断该学员的未通过科目三时间 和缺考时间									
						student.setCfstudentstate(20L);//科目二未通过 科目三未通过									
					}else{//如果全部都为空则说明该学员未开始过科目三
						student.setCfstudentstate(5L);//科目二未通过 科目三未开始									
					}
				}else{//缺考
					/*student.setCfkmebjgsj(updateTime);
					student.setCfstudentstate(5L);//状态为科目二未通过
					student.setCfkmeksqksj(updateTime);//科目二缺考时间*/
					//student.setCfkmekss(_student.getCfkmekss()+1L);//科目二考试次数加一
					student.setCfkmeqkcs((_student.getCfkmeqkcs() == null ? 0:_student.getCfkmeqkcs()) +1L);
					student.setCfkmebjgsj(TimeFormat.timeFormat(new Date()));//科目二未通过时间
					student.setCfkmeksqksj(TimeFormat.timeFormat(new Date()));//科目二缺考时间
					if(_student.getCfkmshgrq() !=null){//先判断科目三通过时间是否存在 
						student.setCfstudentstate(17L);//科目二未通过 科目三通过									
					}else if(_student.getCfkmsbjgsj() !=null || _student.getCfkmsksqksj()!=null){//判断该学员的未通过科目三时间 和缺考时间									
						student.setCfstudentstate(20L);//科目二未通过 科目三未通过									
					}else{//如果全部都为空则说明该学员未开始过科目三
						student.setCfstudentstate(5L);//科目二未通过 科目三未开始									
					}
				}
				//修改
				row2+=studentMapper.updateStudent(student);
			}/*else if(student.getCfkmykss() == 5){//如果考试次数等于5则需要重新交钱考试 把学生状态设置为作废
				//设置学员状态 14为作废
				student.setCfstudentstate(14L);
				student.setCfkmebjgsj(updateTime);//设置科二不及格时间
				//修改
				row2+=studentMapper.updateStudent(student);
			}*/
        }
        if(row1>0 && row2>0){
			return 1;
		}
		return 0;
	}

	@Override
	public int updateSubjectTwoResult(User user, SubjectTwoResult subjectTwoResult, String[] stuid, String[] stufseq,
			String[] stucfzkzmbh, String[] stufnumber, String[] stuname, String[] stucfidcardno, String[] stucfsex,
			String[] stucfsjhm, String[] stucflxdh, String[] stucfslzjcxdh, String[] stucfclazzName,
			String[] stucfamount, String[] stucfreceived, String[] stucfarrearage, String[] stucffavorable,
			String[] stucfvip, String[] stucflocal, String[] stucfexamresult, String[] stucfremark) {
		// TODO Auto-generated method stub
		//修改父类
		int row1 =subjectTwoResultMapper.updateByPrimaryKeySelective(subjectTwoResult);
		//删除子类
		subjectTwoResultentryMapper.deleteByParentId(subjectTwoResult.getFid());
		//添加子类
		int row2=0;
		for (int i = 0; i < stuid.length; i++) {
			SubjectTwoResultentry subjectTwoResultentry=new SubjectTwoResultentry();
			subjectTwoResultentry.setFseq(Long.valueOf(stufseq[i]));
			subjectTwoResultentry.setCfzkzmbh(stucfzkzmbh[i]);
			subjectTwoResultentry.setFid(GetFid.UUIDIntoFid());
			subjectTwoResultentry.setFparentid(subjectTwoResult.getFid());
			subjectTwoResultentry.setCfstudentid(stuid[i]);
			subjectTwoResultentry.setStudentName(stuname[i]);
			subjectTwoResultentry.setCfidcardno(stucfidcardno[i]);
			subjectTwoResultentry.setCfsex(Long.valueOf(stucfsex[i]));
			subjectTwoResultentry.setCfmobile(stucfsjhm[i]);
			subjectTwoResultentry.setCfphone(stucflxdh[i]);
			subjectTwoResultentry.setCfstufnumber(stufnumber[i]);
			subjectTwoResultentry.setCfsqzjcxdh(stucfslzjcxdh[i]);
			subjectTwoResultentry.setCfclazz(stucfclazzName[i]);
			subjectTwoResultentry.setCfreceivable(new BigDecimal(stucfamount[i]));
			subjectTwoResultentry.setCfreceived(new BigDecimal(stucfreceived[i]));
			subjectTwoResultentry.setCfarrearage(new BigDecimal(stucfarrearage[i]));
			subjectTwoResultentry.setCffavorable(new BigDecimal(stucffavorable[i]));
			subjectTwoResultentry.setCfvip(Long.valueOf(stucfvip[i]));
			subjectTwoResultentry.setCfbdyd(Long.valueOf(stucflocal[i]));
			subjectTwoResultentry.setCfexamresult(Long.valueOf(stucfexamresult[i]));
			subjectTwoResultentry.setCfremark(stucfremark[i]);
			row2+=subjectTwoResultentryMapper.insertSelective(subjectTwoResultentry);
			// 添加学员服务日志
			customLogService.addLog("考试管理", "修改了学员:"+subjectTwoResultentry.getCfstufnumber()+subjectTwoResultentry.getStudentName()
					+",科目二考试结果登记!申请车型:"+subjectTwoResultentry.getCfsqzjcxdh()+",考试日期:"+subjectTwoResult.getFbizdate()+",单据编号:"+subjectTwoResult.getFnumber(), subjectTwoResultentry.getCfstudentid(), user.getFid());
		}
		if(row1 >0 && row2 >0){
			return 1;
		}
		return 0;
	}

	@Override
	public List<SubjectTwoResult> selectSubjectTwoResult(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectTwoResultMapper.selectSubjectTwoResult(map);
	}

}
